package com.evil.application.pojo.dto.form.control;

import cn.hutool.core.bean.BeanUtil;
import com.evil.application.pojo.dto.form.control.code.ControlDTO;
import com.evil.application.pojo.dto.form.control.option.FormOptionRespDTO;
import com.evil.common.application.dto.form.control.ControlBaseDTO;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 控件基础信息 实体
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class ControlBaseInfoDTO extends ControlBaseDTO {

    /**
     * 选项列表
     */
    private List<FormOptionRespDTO> options;
    /**
     * 子级控件（表格专用）
     */
    private List<ControlBaseInfoDTO> childControls;

    public ControlBaseInfoDTO() {
        this.options = new ArrayList<>();
        this.childControls = new ArrayList<>();
    }

    public ControlBaseInfoDTO(ControlBaseDTO controlBaseDTO) {
        BeanUtil.copyProperties(controlBaseDTO, this);
        this.options = new ArrayList<>();
        this.childControls = new ArrayList<>();
    }

    public ControlBaseInfoDTO(ControlDTO controlDTO) {
        BeanUtil.copyProperties(controlDTO, this);
        if (null == this.options) {
            this.options = new ArrayList<>();
        }
        this.childControls = new ArrayList<>();
        if (Objects.nonNull(controlDTO)) {
            this.childControls = controlDTO.getChildControls().stream().map(ControlBaseInfoDTO::new).collect(Collectors.toList());
        }
    }
}
